<template>
	<view class="content">
		<view class="top-box">
			<view class="item-top">
				<view class="input-search">
					<input type="text" class="input" placeholder="输入地址,搜索网点" v-model="keywords" />
					<image src="../../static/images/home/search.png" class="image" @click="getGroundMap('move')">
					</image>
				</view>
			</view>
		</view>
		<!-- selectGround&&  @click="showGround =false"-->
		<view class="ground-info">
			<view class="title-box">
				<view>附近网点</view>
				<image src="../../static/images/home/down.png" class="image" v-if="showGround"
					@click="showGround=!showGround"></image>
				<image src="../../static/images/home/up.png" class="image" v-else="showGround"
					@click="showGround=!showGround"></image>
			</view>
			<view class="ground-list" v-if="showGround">
				<view v-for="(item,index) in groundMapList" :key="index" class="ground-item">
					<view class="item-left">
						<view class="item-top">
							<view class="ground-name">{{item.name}}</view>
							<view class="distance">{{item.distance}}km</view>
						</view>
						<view class="item-bottom">{{item.city}} {{item.address}}</view>
					</view>
					<image src="../../static/images/home/map.png" class="item-right" @click.stop="navigatorMap(item)">
					</image>
				</view>
			</view>
		</view>
		<!-- @markertap='getDetail' -->
		<map id="map" class="map" :show-location="true" scale="16" :markers="groundMapList" :latitude="latitude"
			:longitude="longitude"></map>

	</view>
</template>

<script>
	import api from '@/api/index.js';
	export default {
		data() {
			return {
				keywords: '',
				latitude: '',
				longitude: '',
				myLatitude: '',
				myLongitude: '',
				groundMapList: [],
				selectGround: null,
				showGround: true,
				_mapContext: null,
				selected: '',

			}
		},
		onReady() {
			let that = this
			this._mapContext = uni.createMapContext("map", this);
			this._mapContext.getScale({
				success(res) {
					console.log(res)
				}
			})
			this._mapContext.se
			// 仅调用初始化，才会触发 on.("markerClusterCreate", (e) => {})
			this._mapContext.initMarkerCluster({
				enableDefaultStyle: false,
				zoomOnClick: true,
				gridSize: 60,
				complete(res) {
					console.log('initMarkerCluster', res)
					that.openLocation()
				}
			});

			this._mapContext.on("markerClusterCreate", (e) => {
				console.log("markerClusterCreate", e);
			});
		},
		methods: {
			navigatorMap(item) {
				console.log(item)
				uni.openLocation({
					longitude: Number(item.longitude),
					latitude: Number(item.latitude),
					name: item.name,
					address: item.city + ' ' + item.address,
					success: function() {
						console.log('success');
					}
				})
			},
			makePhoneCall(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			getGroundMap(type) {
				api.getGroundMap({
					lat: this.myLatitude,
					lng: this.myLongitude,
					keywords: this.keywords,
				}).then(res => {
					let groundMapList = res.data
					this.groundMapList = groundMapList.map(item => {
						return {
							id: item.id,
							latitude: item.lat,
							longitude: item.lng,
							name: item.name,
							label: {
								content: item.name,
								textAlign: "center"
							},
							distance: item.distance,
							city: item.city,
							address: item.address,
							iconPath: '../../static/images/home/logo.png',
							width: 30,
							height: 30,
							joinCluster: false, // 指定了该参数才会参与聚合
						}
					})
					this.addMarkers(type)
				})
			},
			getDetail(e) {
				console.log(e)
				let index = this.groundMapList.findIndex(item => item.id == e.detail.markerId)
				this.selectGround = this.groundMapList[index]
				this.showGround = true
			},
			myLocation() {
				this._mapContext.moveToLocation({
					latitude: this.myLatitude,
					longitude: this.myLongitude
				})
			},
			addMarkers(type) {
				this._mapContext.addMarkers({
					markers: this.groundMapList,
					clear: true,
					success() {
						console.log('addMarkers成功')
					},
					complete(res) {
						console.log('addMarkers', res)
					}
				})
				if (type == 'move' && this.groundMapList.length > 0) {
					this._mapContext.moveToLocation({
						latitude: Number(this.groundMapList[0].latitude),
						longitude: Number(this.groundMapList[0].longitude)
					})
				}
			},
			// 使用应用内置地图查看位置。
			openLocation() {
				let that = this
				uni.getLocation({
					type: 'gcj02', //返回可以用于uni.openLocation的经纬度
					success: function(res) {
						console.log(res)
						that.myLatitude = res.latitude
						that.myLongitude = res.longitude
						that.myLocation()
						that.getGroundMap()
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.content {
		height: 100%;
		width: 100%;
		position: absolute;

	}

	.ground-info {
		position: absolute;
		bottom: 0px;
		z-index: 100;
		padding: 20rpx;
		width: 100%;
		left: 50%;
		transform: translateX(-50%);
		box-sizing: border-box;
		background: #ffffff;
		border-radius: 10rpx;
		padding: 20rpx;

		.title-box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx 0;

			.image {
				width: 52rpx;
				height: 52rpx;
			}
		}

		.ground-list {
			height: 500rpx;
			overflow-y: scroll;
		}

		.ground-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx 0;
			border-bottom: 2rpx solid #D8D8D8;

			.item-right {
				width: 72rpx;
				height: 72rpx;
			}

			.item-left {
				flex: 1;
				margin-right: 20rpx;
			}

			.item-top {
				display: flex;
				align-items: center;
				// justify-content: space-between;

				.ground-name {
					font-weight: bold;
					font-size: 30rpx;
					margin-right: 20rpx;
				}

				.distance {
					font-size: 18rpx;
					color: #FF4D00;
					min-width: 100rpx;
					height: 40rpx;
					line-height: 40rpx;
					text-align: center;
					background: #FFEAD7;
					padding: 0rpx 10rpx;
				}
			}

			.item-bottom {
				color: #959595;
				margin-top: 20rpx;
			}

		}
	}


	.top-box {
		position: absolute;
		top: 20px;
		z-index: 100;
		padding: 20rpx;
		width: 95%;
		left: 50%;
		transform: translateX(-50%);
		box-sizing: border-box;
		background: #ffffff;
		border-radius: 10rpx;

		.item-bottom {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 20rpx;

			.item-info {
				display: flex;
				align-items: center;
			}

			.value {
				color: #5799ec;
			}

			.image {
				width: 19rpx;
				height: 28rpx;
				margin-right: 10rpx;
			}
		}

		.item-top {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.input-search {
				position: relative;
				flex: 2;

				.input {
					background: #f8f8f8;
					box-shadow: 0rpx 12rpx 116rpx 0rpx rgba(196, 203, 214, 0.1);
					border-radius: 10rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #7d8592;
					padding: 14rpx 40rpx;
					font-style: normal;
				}

				.image {
					width: 48rpx;
					height: 48rpx;
					position: absolute;
					right: 24rpx;
					top: 14rpx;
					z-index: 100;
				}
			}
		}

	}

	.content .map {
		height: 100%;
		width: 100%;
	}
</style>